//
//  ViewController.m
//  EFSMobile
//
//  Created by zhengxianda on 15/12/2.
//  Copyright © 2015年 Elephants Financial Service. All rights reserved.
//

#import "EFSRefreshWebViewController.h"
#import "EFSRefreshView.h"
#import "MJRefreshGifHeader+EFS.h"
#import "NSURL+YTX.h"
#import "EFSWKWebViewController+CommonEvent.h"

#import <ReactiveCocoa/RACEXTScope.h>

@interface EFSRefreshWebViewController ()

@property (nonatomic, assign) BOOL isWebViewLoad;//记录webView第一次加载成功
@property (nonatomic, strong) EFSRefreshView * refreshView;
@end

@implementation EFSRefreshWebViewController

#pragma mark - 懒加载

- (EFSRefreshView *)refreshView{
    if (_refreshView == nil)
    {
        //创建 “刷新失败View” 并配置参数
        _refreshView = [[EFSRefreshView alloc] initWithRefreshStatus:RefreshStatusTypeError];
        
        _refreshView.frame = [[UIApplication sharedApplication] keyWindow].bounds;//框架
        _refreshView.backgroundColor = [UIColor whiteColor];//底色
        @weakify(self)
        _refreshView.refreshButtonTapped = ^{
            @strongify(self)
            [self.webView.scrollView.mj_header beginRefreshing];
        };
    }
    return _refreshView;
}

#pragma mark - 加载控制器

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setRefreshHeader];
    [self handlerNavigate];//注册 handlerNavagete
    [self.webView.scrollView.mj_header beginRefreshing];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.scrollView.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar setAlpha:1];
}

#pragma mark - 加载UI、控件

//添加下拉刷新
- (void)setRefreshHeader{
    //下拉刷新控件绑定事件
    @weakify(self);
    MJRefreshGifHeader *header = [MJRefreshGifHeader EFSHeaderWithRefreshingBlock:^{
        @strongify(self);
        if (self.isWebViewLoad) {//若已加载成功过，则之后调用 reload
            [self.webView reload];
        }
        else {//若未加载成功过，则暂且调用 request
            self.url = [[NSURL URLWithString:self.configUrl] URLByAppendingAllInfo];
        }
    }];
    self.webView.scrollView.mj_header = header;
}

#pragma mark - delegate

- (void)webViewDidFinishLoad:(nonnull UIWebView*)webView{
    [self.webView.scrollView.mj_header endRefreshing];
    [self.refreshView removeFromSuperview];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//    titleLabel.text = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textColor = kCOLOR(blackColor);
    self.navigationItem.titleView = titleLabel;
//    self.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    self.isWebViewLoad = YES;//标记加载成功 - 每次创建只执行一次
//    EFSLog(@"currenturl:%@",self.webView.request.URL.absoluteString);
}
- (void)webView:(nonnull UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    [self.refreshView configRefreshStatus:RefreshStatusTypeError];
    
    [self.webView.scrollView addSubview:self.refreshView];

    [self.webView.scrollView.mj_header endRefreshing];
}
@end
